// David Ciani <dciani@ucsc.edu>
// $Id: Blackjack.java 4 2011-03-08 11:27:57Z dciani@davidciani.com $

import java.util.*;

import static java.lang.System.out;
import static java.lang.System.err;

class Blackjack {

    static Scanner in = new Scanner(System.in);

    static public void main(String args[]) {
        int seed;

        
        while(true){
            try {
                out.print("Enter your lucky number: ");
                seed = in.nextInt();
                break;
            }
            catch(InputMismatchException ex) {
                in.next(); // Clear scanner
                out.println("Invalid Entry.");
            }
        }

        Deck deck = new Deck(seed);

        play(deck);

        out.println("Bye!");
        }

    static void play(Deck deck) {
        //get cmd
        
        char cmd;
        Hand hand = null;

        while(true) {
            try {
                out.print("Enter (n)ew hand, (h)it, or (q)uit: ");
                cmd = in.next().charAt(0);
                
                switch(cmd) {
                    case 'n':
                        hand = new Hand(deck);
                        break;
                    case 'h':
                        try {
                            hand.hit();
                        }
                        catch(HandFullException ex) {
                            out.println("-- ALREADY 5 CARDS IN HAND --");
                        }
                        break;
                    case 'q':
                        return;
                    default:
                        throw new InputMismatchException();
                }

                out.println("You have the following cards:");
                out.println(hand);
                out.println("Value of your hand is: " + hand.value());
                if(cmd == 'n' && hand.value() == 21) out.println("-- BLACKJACK --");
                if(hand.value() > 21) out.println("-- BUSTED --");

                out.println();
            }
            catch(InputMismatchException ex) {
                out.println("-- INVALID INPUT --");
                continue;
            }
            catch(EmptyStackException ex) {
                out.println("-- DECK EMPTY --");
                return;
            }
            catch(HandOverException ex) {
                out.println("-- GET A NEW HAND FIRST --");
                continue;
            }
        }
    }
}
